package com.itshare.video.controller;

import com.itshare.video.aop.annontations.AccountFreezeCheck;
import com.itshare.video.common.annontation.AttrData;
import com.itshare.video.common.annontation.Page;
import com.itshare.video.common.annontation.Size;

import com.itshare.video.common.aop.xss.annontation.CleanXSS;
import com.itshare.video.common.dto.PageEntity;
import com.itshare.video.common.dto.Result;
import com.itshare.video.common.dto.ViewObject;
import com.itshare.video.pojo.bo.VideoCreateBO;
import com.itshare.video.pojo.dto.UserTokenDTO;

import com.itshare.video.pojo.dto.VideoCommentDTO;
import com.itshare.video.pojo.po.VideoComment;
import com.itshare.video.pojo.query.VideoQueryCondition;
import com.itshare.video.service.VideoService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author lyr
 * @create 2020/10/24 14:28
 */
@RestController
@Validated
@RequestMapping("/api/")
public class VideoCreateController {


    @Resource
    private VideoService videoService;


    /**
     * 获取用户发布过的视频
     * @param page
     * @param size
     * @param userTokenDTO
     * @return
     */
    @GetMapping("/user_videos")
    public Result<?> getUserPublishVideo(@Page Integer page, @Size Integer size,@AttrData UserTokenDTO userTokenDTO) {

        return videoService.getUserVideoOfPublish(userTokenDTO,page,size);
    }


    /**
     * 根据 videoID  获取视频详细信息
     * @param videoId
     * @return
     */
    @GetMapping("/user_video")
    public Result<?> getVideoById(@RequestParam Long videoId) {
        return videoService.getVideoById(videoId);
    }






    /**
     * 创建并且发布视频信息
     * @param videoCreateBO
     * @return
     */
    @AccountFreezeCheck
    @PostMapping("/video")
    public Result<?> videoAppCreate(@RequestBody @Validated VideoCreateBO videoCreateBO,
                                    @AttrData UserTokenDTO userTokenDTO) {
        videoCreateBO.setUserTokenDTO(userTokenDTO);
        return videoService.createVideoInfo(videoCreateBO);
    }

    /**
     * 更新上传了的视频信息
     *
     * @param videoCreateBO
     * @param videoId
     * @param userTokenDTO
     * @return
     */
    @CleanXSS
    @AccountFreezeCheck
    @PutMapping("/video/{videoId}")
    public ViewObject updateVideoInfo(@RequestBody VideoCreateBO videoCreateBO,
                                      @PathVariable Long videoId,
                                      @AttrData UserTokenDTO userTokenDTO) {
        //设置 video
        videoCreateBO.setUserTokenDTO(userTokenDTO);
        //设置主键信息
        videoCreateBO.setPk(videoId);
        //更新视频发布信息
        return videoService.updateVideoInfo(videoCreateBO);

    }

    /**
     * 视频搜索
     * @param page
     * @param size
     * @param videoName
     * @param categoryId
     * @return
     */
    @GetMapping("/video/_search")
    public Result<?> searchVideoList(                @Page Integer page,
                                                     @Size Integer size
                                                    ,@RequestParam(required = false) String videoName,
                                                     @RequestParam(required = false) Integer categoryId) {
        VideoQueryCondition queryCondition = VideoQueryCondition.builder()
                .page(page)
                .size(size)
                .categoryId(categoryId)
                .chapterTitle(videoName)
                .build();

        return videoService.searchVideoBy(queryCondition);

    }


    /**
     * 视频点赞
     * @return
     */
    @PutMapping("/video/_like")

    public Result<?> likeVideo(@RequestParam Integer videoId,
                               @AttrData UserTokenDTO userTokenDTO)
    {
        //视频点赞
        return videoService.likeVideo(videoId,userTokenDTO.getId());
    }

    /**
     * 视频评论
     * @return
     */
    @CleanXSS
    @PutMapping("/video/_comment")
    public Result<?> commentVideo( @AttrData @Validated UserTokenDTO userTokenDTO,
                                  @RequestBody @Validated VideoCommentDTO videoCommentDTO,
                                  @RequestParam Long videoId) {
        videoCommentDTO.setFromUserId(userTokenDTO.getId());
        return videoService.commentVideo(videoCommentDTO,videoId,userTokenDTO);

    }

    /**
     * 获取一级评论
     * @param videoId
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/video/_comment")
    public ViewObject getComment(@RequestParam Integer videoId, @Page Integer page, @Size Integer size) {

        return videoService.getVideoComment(videoId,page,size);

    }

    /**
     * 获取二级评论
     * @param page
     * @param size
     * @param parentCommentId
     * @param videoId
     * @return
     */
    @GetMapping("/video/_comment/childs")
    public Result<PageEntity<VideoComment>> getVideoComment(@Page Integer page,@Size Integer size
                                                            ,@RequestParam Integer parentCommentId
                                                            ,@RequestParam Integer videoId) {
        return videoService.getVideoCommentChilds(parentCommentId,videoId,page,size);

    }





    // /**
    //  * 获取视频详情页
    //  * @return
    //  */
    // @GetMapping("/video/_detail")
    // public ViewObject getDetailOfVideo(@RequestParam Integer videoId) {
    //
    // }



}
